package com.yupi.yu_picture.api.imageSearch.sub;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import com.yupi.yu_picture.exception.BusinessException;
import com.yupi.yu_picture.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Error;
import org.checkerframework.checker.units.qual.A;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class GetImagePageUrlApi {

    public  static String getImagePageUrl(String imageUrl){
        //抓取数据 也就是拿到另一个网站的请求接口 向这个接口发请求得到响应即可
        //1. 构造请求参数
        Map<String,Object> formData=new HashMap<>();
        formData.put("image",imageUrl);
        formData.put("tn","pc");
        formData.put("from","pc");
        formData.put("image_source","PC_UPLOAD_URL");

        //获取当前时间戳
        long uptime = System.currentTimeMillis();
        //请求地址
        String url="https://graph.baidu.com/upload?uptime=" + uptime;
        String Acs_token="az6g8Ofj7p7kDxrXg5M/SgufGru/1pHNQeSNKpyeBnH3krbBnrRu5abpo59muN56rW3vc+6CpuBsgeOv5WHjzVbrIcb8DWxxsxVBfaFQU902DDVo5YMMUMrB9iKpTRhomL7jcMFhkvtJcOoDdcWyOy9JHfsuqOu4mi2dz5xCGTjTEfmbW3p5/yu1i2S+WsAfXYppCX2A5V6aVjo8zpUw3qyGYx/U2BTwKAOpkgghEDztTvgSoOOb2laoxre6oVW5U2jx0sd66qt9t/bjvPe4HngL+c/Kz6Jj576gs/UmhX1rEx7MuCHjF8BTYXlQXcvB614u5nuMZWN0gJbPJavSg9bMZiW6W1AJSB8sLSLiDN72D6MQER/zIKc4kCAq4pLlZ9BfCo0W4Zf8xvycoNqp9gYaxEmWQALdDta0XBkOoe26SVEjAvMIJ/PkIVvvDVTQnYM7oCuTCRhqv7I4+Iohu87hI/zxbX8r8bDke/9n9uc=";


        HttpResponse response = HttpRequest.post(url)
                .form(formData)
                .timeout(5000)
                .header("Acs_Token", Acs_token)
                .execute();
        //判断响应状态
        try {
            if (HttpStatus.HTTP_OK != response.getStatus()) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败");
            }
            //解析响应
            String responseBody = response.body();
            Map<String, Object> result = JSONUtil.toBean(responseBody, Map.class);

            //3.处理响应结果
            if (result == null || !Integer.valueOf(0).equals(result.get("status"))) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "接口调用失败");
            }
            Map<String, Object> data = (Map<String, Object>) result.get("data");
            String rawUrl = (String) data.get("url");
            //对URL进行解码
            String searchResultUrl = URLUtil.decode(rawUrl, StandardCharsets.UTF_8);
            if (searchResultUrl == null) {
                throw new BusinessException(ErrorCode.OPERATION_ERROR, "未返回有效结果");
            }
            return searchResultUrl;
        }catch (Exception e){
            log.info("以图搜图调用失败");
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"搜索失败");
        }

    }


    public static void main(String[] args) {
        String imageUr="https://www.codefather.cn/logo.png";
        String pageUrl = getImagePageUrl(imageUr);
        System.out.println(pageUrl);
    }
}
